package ext.generic.integration.sourcing.util;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import com.ptc.core.lwc.server.LWCEnumerationEntryValuesFactory;
import com.ptc.core.lwc.server.LWCNormalizedObject;
import com.ptc.core.meta.common.DataSet;
import com.ptc.core.meta.common.DisplayOperationIdentifier;
import com.ptc.core.meta.common.EnumeratedSet;
import com.ptc.core.meta.common.EnumerationEntryIdentifier;
import com.ptc.core.meta.common.impl.LogicalAttributeFormatException;
import com.ptc.core.meta.container.common.AttributeTypeSummary;

import ext.com.iba.IBAUtil;
import ext.generic.erp.common.CommonPDMUtil;
import ext.tianma.part.util.ClassificationUtil;
import wt.meta.LocalizedValues;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.util.WTException;

public class PartAttribute {

	private static LWCNormalizedObject object;
	private static Locale locale;
	// 零部件名称
	private static final String PARTNAME = "name";
	// 零部件编码
	private static final String PARTNUMBER = "number";
	// 零部件生命周期
	private static final String LIFECYCLE = "lifecycle";
	// 零部件生命周期
	private static final String CLASSIFICATIONPATH = "classificationPath";
	// 零部件默认单位
	private static final String UNIT = "defaultUnit";
	// 零部件默认单位
	private static final String VIEW = "view";
	// 零部件大版本
	private static final String MAJOR_VERSION = "majorVersion";
	// 零部件小版本
	private static final String MINOR_VERSION = "minorVersion";
	// 零部件版本
	private static final String VERSION = "Version";
	// 规格补充信息
	private static final String PART_MAIN_SPECIFICATION = "partMainSpecification";
	// 记录规格补充信息穿的字段信息
	private static final String ALL_SUPPLY_Property = "allSupplyProperty";

	public static String getAttributeValue(WTPart part, String attribute) {
		System.out.println("attribute is: " + attribute);
		String value = "";
		try {
			if (PARTNAME.equals(attribute)) {
				value = part.getName();
			} else if (PARTNUMBER.equals(attribute)) {
				value = part.getNumber();
			} else if (LIFECYCLE.equals(attribute)) {
				value = CommonPDMUtil.getLifecycle(part);
			} else if (UNIT.equals(attribute)) {
				value = CommonPDMUtil.getERPMappingUnit(part);
			} else if (MAJOR_VERSION.equals(attribute)) {
				value = CommonPDMUtil.getMajorVersion(part);
			} else if (MINOR_VERSION.equals(attribute)) {
				value = CommonPDMUtil.getMinorVersion(part);
			} else if (VERSION.equals(attribute)) {
				value = CommonPDMUtil.getMinorVersion(part);
			}else if (VIEW.equals(attribute)) {
				value = part.getViewName();
			} else if (CLASSIFICATIONPATH.equals(attribute)) {
				value = ClassificationUtil.queryClassificationPath(part);
			} else if (PART_MAIN_SPECIFICATION.equals(attribute)) {
				Object supplyProperty = IBAUtil.getIBAValue(part,ALL_SUPPLY_Property);
				System.out.println("supplyProperty : " + supplyProperty);
				if (supplyProperty != null) {
					String supplyPropertyValue =(String) supplyProperty;
					String[] properties = supplyPropertyValue.trim().split("&");
					value = jointSoftProperty(part, properties);
				}
			} else {

				value = getSoftProperty(part, attribute);
			}
		} catch (WTException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		return value;
	}

	private static String jointSoftProperty(WTPart part, String[] properties) {
		List<String> list = new ArrayList<String>();
		String value = "";
		for (String property : properties) {
			String propertyValue = getSoftProperty(part, property).trim();
			if (StringUtils.isNotBlank(propertyValue)) {
				list.add(propertyValue);
			}
		}
		int size = list.size();
		if (size > 0) {
			value = StringUtils.join(list, '&');
		}
		return value;
	}

	private static String getSoftProperty(WTPart part, String attribute) {
		String value = "";
		System.out.println("soft attribute is: " + attribute);
		try {
			locale = SessionHelper.manager.getLocale();
			object = new LWCNormalizedObject(part, null, locale,
					new DisplayOperationIdentifier());
			object.load(attribute);
			Object obj = object.get(attribute);
			AttributeTypeSummary attributeTypeSummary = object.getAttributeDescriptor(attribute);
			DataSet dataSet = attributeTypeSummary.getLegalValueSet();
			if (dataSet != null) {
				if ((dataSet instanceof EnumeratedSet)) {
					EnumerationEntryIdentifier eei = ((EnumeratedSet) dataSet)
							.getElementByKey(obj.toString());
					LWCEnumerationEntryValuesFactory eevf = new LWCEnumerationEntryValuesFactory();
					LocalizedValues localValue = eevf.get(eei, locale);
					value = localValue.getDisplay();
				}
			} else {
				value = obj.toString();
			}
		} catch (LogicalAttributeFormatException e) {
			return value;
		} catch (NullPointerException e) {
			return value;
		} catch (Exception e) {
			return value;
		} 
		System.out.println("attribute localized value is: " + value);
		return value;
	}
}
